package com.example.CarFee;

import java.util.ArrayList;

import android.util.Log;

public class Service {
	static String currentAccount;
	static String currentPassword;
	private String[] placeArray = { "广州", "深圳", "信宜", "汕尾", "汕头", "潮州", "揭阳",
			"梅州", "阳江" };

	// 登陆成功会返回true，失败返回false
	public static boolean denglu(String currentAccount, String currentPassword) {
		String response = httpUtil.sendPost(
				"http://1.lth707.sinaapp.com/api.php" + "?accout="
						+ currentAccount + "&password=" + currentPassword
						+ "&action=" + "DataSeek", "").trim();
		if (response.equals("true")) {
			// System.out.println("登陆成功");
			return true;

		} else {
			// System.out.println("登陆失败");
			return false;
		}
	}

	// 注册成功返回真
	public static boolean zhuce(String a, String b) {
		String response = httpUtil.sendPost(
				"http://1.lth707.sinaapp.com/api.php" + "?accout=" + a
						+ "&password=" + b + "&action=" + "DataAdd", "").trim();
		if (response.equals("注册成功")) {
			// System.out.println("注册成功");
			return true;
		} else {
			// System.out.println("账号已经被注册过");
			return false;
		}
	}

	// 返回一个数组第一个组元存姓名，第二个存性别，第三个存出生地，第四个存订票情况
	public static String[] checkPersonalInfo() {
		String response = httpUtil.sendPost(
				"http://1.lth707.sinaapp.com/api.php" + "?accout="
						+ currentAccount + "&password=" + currentPassword
						+ "&action=" + "checkPersonalInfo", "").trim();
		// System.out.println(response);
		String[] info = response.split(",");
		String[] ziduan = { "姓名", "性别", "出生地", "订票情况", "汽车站", "出发日期", "出发时间",
				"手机号码" };
		for (int i = 0; i < info.length; i++) {
			System.out.print(ziduan[i] + ":" + info[i] + "  ");
		}
		return info;
	}

	// 添加或修改成功返回真
	public static boolean AddPersonalInfo(String name, String sex,
			String place, String phone) {
		String response = httpUtil.sendPost(
				"http://1.lth707.sinaapp.com/api.php" + "?accout="
						+ currentAccount + "&password=" + currentPassword
						+ "&name=" + name + "&sex=" + sex + "&place=" + place
						+ "&phone=" + phone + "&action=" + "AddPersonInfo", "")
				.trim();
		// System.out.println(response);
		if (response.equals("添加成功")) {
			System.out.println("添加成功");
			return true;
		} else if (response.equals("修改成功")) {
			System.out.println("修改成功");
			return true;
		} else if (response.equals("请先登录您的账号")) {
			System.out.println("请先登录您的账号");
		}
		return false;
	}

	// 新增或修改订票，成功会返回真
	public static boolean BuyCarTicket(String startPlace, String endPlace,
			String station, String date, String time) {
		String response = httpUtil.sendPost(
				"http://1.lth707.sinaapp.com/api.php" + "?accout="
						+ currentAccount + "&password=" + currentPassword
						+ "&startPlace=" + startPlace + "&endPlace=" + endPlace
						+ "&station=" + station + "&date=" + date + "&time="
						+ time + "&action=" + "BuyCarTicket", "").trim();
		// System.out.println(response);
		if (response.equals("订票成功")) {
			System.out.println("订票成功");
			return true;
		} else if (response.equals("改签成功")) {
			System.out.println("改签成功");
			return true;
		} else if (response.equals("请先登录您的账号")) {
			System.out.println("请先登录您的账号");
		}
		return false;
	}

	// 发布的函数，发布成功返回真
	public static boolean Release(String content) {
		String response = httpUtil.sendPost(
				"http://1.lth707.sinaapp.com/api.php" + "?accout="
						+ currentAccount + "&content=" + content + "&action="
						+ "Release", "").trim();
		// System.out.println(response);
		if (response.equals("发布成功")) {
			System.out.println("发布成功");
			return true;
		} else if (response.equals("请先登录您的账号")) {
			System.out.println("请先登录您的账号");
		}
		return false;
	}

	// 返回消息的数组
	public static String[] RefreshInfoList() {
		String response = httpUtil.sendPost(
				"http://1.lth707.sinaapp.com/api.php" + "?accout="
						+ currentAccount + "&action=" + "RefreshInfoList", "")
				.trim();
		// System.out.println(response);
		String[] info = response.split(",");
		// String [] ziduan={"姓名","性别","出生地","订票情况"};
		for (int i = 0; i < info.length; i++) {
			System.out.print(info[i] + "\r\n");
		}
		return info;
	}

	// 返回他人信息的数组
	public String[] checkOtherInfo(String Account) {
		String response = httpUtil.sendPost(
				"http://1.lth707.sinaapp.com/api.php" + "?accout=" + Account
						+ "&action=" + "checkOtherInfo", "").trim();
		// System.out.println(response);
		String[] info = response.split(",");
		String[] ziduan = { "姓名", "性别", "出生地" };
		for (int i = 0; i < info.length; i++) {
			System.out.print(ziduan[i] + ":" + info[i] + " ");
		}
		return info;
	}

	// 发布的评论，发布成功返回真，第一个为当前登陆账号，第二个为评论的消息，第三个为评论
	public static boolean Review(String content, String reviewContent) {
		String response = httpUtil.sendPost(
				"http://1.lth707.sinaapp.com/api.php" + "?accout="
						+ currentAccount + "&content=" + content
						+ "&reviewContent=" + reviewContent + "&action="
						+ "Review", "").trim();
		// System.out.println(response);
		if (response.equals("评论成功")) {
			System.out.println("评论成功");
			return true;
		} else if (response.equals("请先登录您的账号")) {
			System.out.println("请先登录您的账号");
		}
		return false;
	}

	// 返回一个评论列表
	public static ArrayList<String> checkReview(String content) {
		String response = httpUtil.sendPost(
				"http://1.lth707.sinaapp.com/api.php" + "?accout="
						+ currentAccount + "&content=" + content + "&action="
						+ "checkReview", "").trim();
		System.out.println(response);
		String[] info = response.split(",");
		// String [] ziduan={"姓名","性别","出生地"};
		// int j=0;
		ArrayList<String> infoList = new ArrayList<String>();
		for (int i = 0; i < info.length; i++) {
			if (!info[i].equals("")) {
				infoList.add(info[i]);
				// System.out.print(j+++":"+info[i]+" ");
			}
		}
		for (String a : infoList) {
			System.out.println(a);
		}
		return infoList;
	}

	// 退票成功返回真
	public static boolean CancelTicket() {
		String response = httpUtil.sendPost(
				"http://1.lth707.sinaapp.com/api.php" + "?accout="
						+ currentAccount + "&password=" + currentPassword
						+ "&action=" + "CancelTicket", "").trim();
		if (response.equals("退票成功")) {
			System.out.println("退票成功");
			return true;
		} else if (response.equals("请先登录您的账号")) {
			System.out.println("请先登录您的账号");
		}
		return false;
	}

	public static int CheckRemainTicket(String startPlace, String endPlace) {
		String response = httpUtil.sendPost(
				"http://1.lth707.sinaapp.com/api.php" + "?startPlace="
						+ startPlace + "&endPlace=" + endPlace + "&action="
						+ "CheckRemainTicket", "").trim();
		// Log.i("ssss", response1);
		System.out.println(response);
		int a = Integer.parseInt(response);
		return a;
	}

}
